exports.title = "疯狂下拉";
exports.description = "qq下拉抢红包等可以使用\n先打开想要疯狂下拉的app，再打开手替app，打开本脚本，点击执行；本脚本会切换到刚刚打开的app并不断下/上拉\n";
exports.package = "all";
exports.params = [{
    key: "direct",
    name: "方向(上/下)"
}, {
    key: "second",
    name: "时长(秒)",
    value: "5"
}];

exports.script = function(shell, param) {
    return {
        TYPE_WINDOW_STATE_CHANGED(data) {
			console.log(data);
            // 窗口切换
            switch (this.flag) {
                case "opening":
                    if (this.flag == "opening" && data.packageName == this.packageName) {
                        console.log("进入应用，开始下拉");
                        this.flag = "running";
                        this.pull();
                    }
                    break;
                case "running":
                    this.stop();
                    break;
                default:
                    if (/\.launcher$/.test(data.packageName)) {

                    } else if (["cn.inu1255.threehands", "com.android.systemui"].indexOf(data.packageName) < 0) {
                        console.log("更新最近使用的应用", data.packageName);
                        this.packageName = data.packageName;
                    }
            }
        },
        pull() {
            var prev = new Date().getTime();
            var that = this;
            var s = shell.getSize();
            s = s.split(",");
            var x = s[0] / 2;
            var y = s[1] / 2;
            var y1 = y + (param.direct == "上" ? -100 : 100);
            var count = 0;
            var run = function() {
                if (that.flag == "running" && new Date().getTime() - prev < param.second * 1e3) {
                    console.log("下拉", x, y, x, y1, 20);
                    shell.swipe(x, y, x, y1, 20);
                    setTimeout(run, 20);
                    count++;
                } else {
                    this.flag = "stop";
                    console.log(count, "次");
                }
            };
            run();
        },
        stop() {
            console.log("已停止");
            this.flag = "stop";
        },
        run: function() {
            this.flag = "opening";
            if (this.packageName) {
                shell.open(this.packageName);
            } else {
                shell.toast("请先打开要疯狂下拉的目标");
            }
            // shell.doAction(3);
        }
    };
};